package com.huawei.datastructurelearning.二分查找;

public class Code1 {
    /**
     * 给定有序数组A 查找值 target 找到返回索引 找不到 返回-1
     */
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 7, 9, 11, 12, 14, 15};
        int target = 16;
        System.out.println(searchTarget(nums, target));
    }

    private static int searchTarget(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (true) {
            if (left > right) {
                break;
            }
            int middle = (left + right) >>> 1;
            if(nums[middle]==target){
                return middle;
            }else if(nums[middle]>target){
                right = middle-1;
            }else {
                left = middle+1;
            }
        }
        return -1;
    }


}
